package com.solcoo.common.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.widget.ImageView;

/**
 * 
 * @author hongfa.yy
 * @version 2013-1-14 上午11:32:37
 */
public class HCropView extends ImageView {
	private RectF mRect;

	public HCropView(Context context, RectF rect, float rectH, float rectW,
			float minRectH, float minRectW) {
		super(context);
		init(rect, rectH, rectW, minRectH, minRectW);
	}

	public void init(RectF rect, float rectH, float rectW, float minRectH,
			float minRectW) {
		this.mRect = rect;
		this.rectH = rectH;
		this.rectW = rectW;
		this.minRectH = minRectH;
		this.minRectW = minRectW;
		rect0.left = (mRect.right + mRect.left) / 2 - rectW / 2;
		rect0.top = (mRect.bottom + mRect.top) / 2 - rectH / 2;
		rect0.right = rect0.left + rectW;
		rect0.bottom = rect0.top + rectH;
	}

	private float rectH;
	private float rectW;
	private float minRectH;
	private float minRectW;
	private RectF rect0 = new RectF();

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Paint mPaint = new Paint();
		mPaint.setStyle(Style.STROKE);
		// mPaint.setStrokeMiter(6);
		mPaint.setStrokeWidth(3);
		mPaint.setColor(Color.RED);
		canvas.drawRect(filter(rect0), mPaint);

		mPaint.reset();
		mPaint.setStyle(Style.FILL);
		mPaint.setColor(Color.BLACK);
		mPaint.setAlpha(100);
		RectF rect1 = new RectF(mRect);
		rect1.right = rect0.left;
		canvas.drawRect(rect1, mPaint);

		RectF rect2 = new RectF(rect0);
		rect2.top = mRect.top;
		rect2.bottom = rect0.top;
		canvas.drawRect(rect2, mPaint);

		RectF rect3 = new RectF(mRect);
		rect3.left = rect0.right;
		canvas.drawRect(rect3, mPaint);

		RectF rect4 = new RectF(rect0);
		rect4.top = rect0.bottom;
		rect4.bottom = mRect.bottom;
		canvas.drawRect(rect4, mPaint);

	}

	private RectF filter(RectF rect) {
		if (rect.left < mRect.left)
			rect.left = mRect.left;
		if (rect.top < mRect.top)
			rect.top = mRect.top;
		if (rect.bottom > mRect.bottom)
			rect.bottom = mRect.bottom;
		if (rect.right > mRect.right)
			rect.right = mRect.right;
		return rect;
	}

	private final int NONE = 0;
	private int mode = NONE;
	private final int MOVE = 1;
	private final int ZOOM = 2;
	private float startX;
	private float startY;
	private float SPACE = 10f;
	private float zoomDist = 1f;

	/*** 两点的距离 */
	private float spacing(MotionEvent event) {
		float x = event.getX(0) - event.getX(1);
		float y = event.getY(0) - event.getY(1);
		return FloatMath.sqrt(x * x + y * y);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction() & MotionEvent.ACTION_MASK) {
		case MotionEvent.ACTION_DOWN:
			startX = (int) event.getX();
			startY = (int) event.getY();
			mode = NONE;
			if (rect0.contains(startX, startY)) {
				mode = MOVE;
				return true;
			}
			if (mRect.contains(startX, startY)) {
				return true;
			}

			break;
		case MotionEvent.ACTION_POINTER_DOWN:
			zoomDist = spacing(event);
			if (zoomDist > SPACE) {
				mode = ZOOM;
				return true;
			}
			break;
		case MotionEvent.ACTION_UP:
			mode = NONE;
			break;
		case MotionEvent.ACTION_POINTER_UP:
			mode = NONE;
			break;
		case MotionEvent.ACTION_MOVE:
			if (mode == MOVE) {
				float x = event.getX();
				float y = event.getY();
				// view
				float xx = rect0.left + x - startX;
				float yy = rect0.top + y - startY;
				if (xx < mRect.left)
					xx = mRect.left;
				else if (xx > mRect.right - rectW)
					xx = mRect.right - rectW;
				if (yy < mRect.top)
					yy = mRect.top;
				else if (yy > mRect.bottom - rectH)
					yy = mRect.bottom - rectH;
				rect0.offsetTo(xx, yy);
				startX = x;
				startY = y;
				invalidate();
				return true;
			} else if (mode == ZOOM) {
				float factor = spacing(event) / zoomDist;
				rectW = rectW * factor;
				rectH = rectH * factor;
				// 最大的时候进行限制 等比例缩放
				if (rectW < minRectW)
					rectW = minRectW;
				if (rectH < minRectH)
					rectH = minRectH;
				float w0 = rectW / (mRect.right - mRect.left);
				float h0 = rectH / (mRect.bottom - mRect.top);
				float wh = w0 > h0 ? w0 : h0;
				if (wh > 1f) {
					rectW = rectW / wh;
					rectH = rectH / wh;
				}
				// 计算方式

				float midX = (rect0.left + rect0.right) / 2;
				float midY = (rect0.top + rect0.bottom) / 2;
				rect0.left = midX - rectW / 2;
				rect0.right = midX + rectW / 2;
				rect0.top = midY - rectH / 2;
				rect0.bottom = midY + rectH / 2;
				invalidate();
				return true;
			}
			break;
		default:
			break;
		}
		return super.onTouchEvent(event);
	}

	public RectF getRectF() {
		return rect0;
	}

	public RectF getBitmapRectF() {
		return mRect;
	}
}
